package pl.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import pl.model.domain.Book;
import pl.model.service.SearchService;
import pl.model.service.impl.SearchServiceImpl.SearchResult;

@Controller
public class SearchController {

	protected SearchService searchService;
	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	protected final Log log = LogFactory.getLog(getClass());
	
	@RequestMapping("/search")
	public ModelMap search(){
		Integer page_no = 1;
		Integer page_size = 5;
		ModelMap model = new ModelMap();
		model.addAttribute("page_no", page_no);
		model.addAttribute("page_size", page_size);
		tryCatpcha();
		return model;
	}
	
	private void tryCatpcha() {
		log.debug("try captcha");
	}

	@RequestMapping("/search_query")
	public ModelMap search_query(@RequestParam(required=false) String keywords, 
			 @RequestParam(required=false) String search_type, 
			 @RequestParam(required=false) String [] search_by,
			 @RequestParam(required=false) String rank_value, 
			 @RequestParam String sort_by, @RequestParam Integer page_size, @RequestParam Integer page_no){
			log.info("search_query");
		log.info("keywords: " + keywords + " search_type " + search_type +
				" search_by " + StringUtils.join(search_by, " ") 
				+ " rv " + rank_value + " sort_by " + sort_by);
		SearchResult sresult = searchService.searchBookUniversal(search_type, 
				search_by, keywords, rank_value, sort_by, page_size, page_no);
		List<Book> books = sresult.books;
		log.info("size " + books.size());
		Integer page_num = sresult.size/page_size;
		if (sresult.size%page_size>0) {
			log.debug("sresult.size%page_size " + sresult.size%page_size + " "  );
			page_num++;
		}
		ModelMap model = new ModelMap("books", books);
		model.addAttribute("page_no", page_no);
		model.addAttribute("page_size", page_size);
		model.addAttribute("page_num", page_num);
		return model;
	}
	
	@RequestMapping("searchBooksByTitle")
	public ModelMap searchBooksByTitle(@RequestParam String keywords){
		List<Book> books = searchService.searchBookByTitle(keywords);
		return new ModelMap("books", books);
	}
	
	@RequestMapping("searchBooksByDescription")
	public ModelMap searchBooksByDescription(@RequestParam String [] keywords){
		List<Book> books = searchService.searchBookByDescription(keywords);
		return new ModelMap("books", books);
	}
	
	@RequestMapping("searchBookByTags")
	public ModelMap searchBooksByTags(@RequestParam String [] keywords){
		List<Book> books = searchService.searchBookByTags(keywords);
		return new ModelMap("books", books);
	}
	
//	@RequestMapping("searchBooksByTagsTitleDescription")
//	public ModelMap searchBooksByTagsTitleDescription(@RequestParam String keywords){
//		List<Book> books = searchService.searchBooksByTagsTitleDescription(keywords);
//		return new ModelMap("books", books);
//	}
}
